﻿// SimulateWnd.cpp : 实现文件

// 继承HWnd后不能使用onpaint消息

#include "stdafx.h"
#include "Simulate.h"
#include "../Hui/Hui.h"
#include "SimulateWnd.h"

void CALLBACK SimulateWnd::myTimer(	// 必须要静态或全局的
	HWND hWnd,      // handle of CWnd that called SetTimer
	UINT nMsg,      // WM_TIMER
	UINT_PTR nIDEvent,   // timer identification 指向this
	DWORD dwTime    // system time
	)
{
	((SimulateWnd*)nIDEvent)->Invalidate();
	((SimulateWnd*)nIDEvent)->PostMessage( WM_COMMAND, 0 );	// 发送一个没用的消息，激活工位按钮状态，?? 改用标准方法
}


// SimulateWnd

IMPLEMENT_DYNAMIC(SimulateWnd, HWnd)

SimulateWnd::SimulateWnd()
: HWnd( 300, 300 )	// 主控件最小800x600
, m_pSimulateCtrl(0)
{
}

SimulateWnd::~SimulateWnd()
{
}

BEGIN_MESSAGE_MAP(SimulateWnd, HWnd)
	ON_COMMAND(TID_FULLSCREEN, OnFullScreen)	// 全屏切换
	ON_COMMAND(TID_EXIT, OnExit)
	ON_WM_CLOSE()
	ON_WM_SYSCOMMAND()
END_MESSAGE_MAP()


// SimulateWnd 消息处理程序

BOOL SimulateWnd::CreateEx(DWORD dwExStyle, LPCTSTR lpszClassName, LPCTSTR lpszWindowName, DWORD dwStyle, const RECT& rect, CWnd* pParentWnd, UINT nID, LPVOID lpParam)
{
	//m_ctrFrame.setTestColor();	// 强制显示控件背景色，用于判断重画区域是否合理高效，测试用，为0时不显示。
	//m_ctrFrame.setBackColor( RGB(60, 55, 40) );
		
	// 场地，
	m_pSimulateCtrl = new SimulateCtrl( CRect(0, 0, 0, 0) );	// 在onsize中自适应大小
	m_pSimulateCtrl->setDock( HDOCK_FULLHORIZONTAL | HDOCK_FULLVERTICAL );
	m_ctrFrame.addCtrl( m_pSimulateCtrl );
	
	// 后建窗口
	if( !HWnd::CreateEx(dwExStyle, lpszClassName, lpszWindowName, dwStyle, rect, pParentWnd, nID, lpParam) )
		return FALSE;

	// 载入快捷键资源
	//setAccelerators( IDR_MAINFRAME );

	SetTimer( (UINT_PTR)this, 60, myTimer );	// 定时刷新

	return TRUE;
}

// 全屏切换
void SimulateWnd::OnFullScreen()
{
	theApp.setFullScreen( !theApp.getFullScreen() );
	showWindow();
}

// 退出
void SimulateWnd::OnExit()
{
	PostMessage(WM_CLOSE);
}

void SimulateWnd::OnClose()
{
	// 保存窗口位置
	WINDOWPLACEMENT wp;
	GetWindowPlacement( &wp );	// 取窗口的位置、大小及最大化最小化等
	theApp.WriteInt( L"WindowLeft", wp.rcNormalPosition.left );
	theApp.WriteInt( L"WindowTop", wp.rcNormalPosition.top );
	theApp.WriteInt( L"WindowRight", wp.rcNormalPosition.right );
	theApp.WriteInt( L"WindowBottom", wp.rcNormalPosition.bottom );
	//theApp.WriteInt( L"ShowMaximized", theApp.getMaximized() );

	// 保存数据
	m_pSimulateCtrl->saveData();

	HWnd::OnClose();
}

// 根据状态显示窗口
void SimulateWnd::showWindow()
{
	if( theApp.getFullScreen() )		// 从窗口到全屏模式
	{
		ModifyStyle( WS_OVERLAPPEDWINDOW, WS_POPUP);
		ShowWindow( SW_SHOWMAXIMIZED );
	}
	else		// 从全屏到窗口模式
	{
		ModifyStyle( WS_POPUP, WS_OVERLAPPEDWINDOW);
		ShowWindow( SW_RESTORE );
	}

	theApp.WriteInt(L"FullScreen", theApp.getFullScreen());
}

void SimulateWnd::OnSysCommand(UINT nID, LPARAM lParam)
{
	if( nID == SC_MAXIMIZE )
	{
		theApp.setFullScreen( TRUE );
		showWindow();
	}
	else if( nID == SC_RESTORE )
	{
		theApp.setFullScreen( FALSE );
		showWindow();
	}

	HWnd::OnSysCommand(nID, lParam);
}
